package com.example.netty.http_demo;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 服务端
 */
public class HelloServer {
    public static void main(String[] args) throws  Exception{
        //创建 主线程组，主线程接收并把任务丢给从线程，从线程做处理
        EventLoopGroup bossGrorp=new NioEventLoopGroup();
        //从线程组
        EventLoopGroup workerGrorp=new NioEventLoopGroup();

        // netty服务器创建，ServerBootstrap是一个启动类
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        serverBootstrap.group(bossGrorp,workerGrorp)      //设置主从线程
                .channel(NioServerSocketChannel.class)   //  设置nio的双向管道
                .childHandler(new HelloServerInitializer());                      //子处理器

        try {
            //启动server并绑定端口监听和设置同步方式
            ChannelFuture channelFuture= serverBootstrap.bind(8080).sync();

            //关闭监听方式
            channelFuture.channel().closeFuture().sync();
        }finally {
            bossGrorp.shutdownGracefully();
            workerGrorp.shutdownGracefully();
        }

    }
}
